package com.hll.springcrud.controller;

import com.hll.springcrud.dao.StudentDao;
import com.hll.springcrud.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class StudentController {

    @Resource
    private StudentDao studentDao;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Student>list=studentDao.findAll();
        model.addAttribute("li",list);
        return "main";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam("sid") int sid){
        studentDao.delete(sid);
        return "redirect:/findAll";
    }
    /**
     * 查一个
     */
    @RequestMapping("/findOne")
    public String findOne(@RequestParam("sid") int sid,Model model){
        Student one = studentDao.findOne(sid);
        model.addAttribute("one",one);
        return "update";
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public String update(Student student){
        studentDao.update(student);
        return "redirect:/findAll";
    }

    /**
     * 去添加
     * @return
     */
    @RequestMapping("/toInsert")
    public String toInsert(){
        return "insert";
    }
    /**
     * 添加方法
     */
    @RequestMapping("/insert")
    public String insert(Student student){
        studentDao.insert(student);
        return "redirect:/findAll";
    }



}
